package com.example.demo.bean;

import java.util.HashMap;
import java.util.Objects;

public class AjaxRes extends HashMap<String, Object> {
    /**
     * 状态码
     */
    public static final String CODE_TAG = "code";

    /**
     * 返回内容
     */
    public static final String MSG_TAG = "msg";

    /**
     * 数据对象
     */
    public static final String DATA_TAG = "data";

    public AjaxRes() {
    }

    public AjaxRes(int code, String msg) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    public AjaxRes(int code, String msg, Object data) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (Objects.nonNull(data)) {
            super.put(DATA_TAG, data);
        }
    }

    public static AjaxRes success() {
        return AjaxRes.success("操作成功");
    }

    public static AjaxRes success(String msg) {
        return AjaxRes.success(msg, null);
    }

    public static AjaxRes success(Object data) {
        return AjaxRes.success("操作成功", data);
    }

    public static AjaxRes success(String msg, Object data) {
        return new AjaxRes(200, msg, data);
    }

    public static AjaxRes error() {
        return AjaxRes.error("操作失败");
    }

    public static AjaxRes error(String msg) {
        return AjaxRes.error(msg, null);
    }

    public static AjaxRes error(Object data) {
        return AjaxRes.error("操作失败", data);
    }

    public static AjaxRes error(String msg, Object data) {
        return new AjaxRes(500, msg, data);
    }

    public static AjaxRes warn(String msg) {
        return AjaxRes.warn(msg, null);
    }

    public static AjaxRes warn(String msg, Object data) {
        return new AjaxRes(600, msg, data);
    }

    @Override
    public AjaxRes put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
